package com.e3.sysmg.security.dao;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import com.e3.core.base.SuperDAO;
import com.e3.sysmg.security.entity.ModuleEntity;
import com.e3.sysmg.security.model.ModuleModel;
import com.e3.sysmg.security.model.ModuleQC;

public class ModuleDAO extends SuperDAO {

	private String querySQL = "SELECT m FROM ModuleEntity m";
	
	public List<ModuleEntity> getModulesByCondition(ModuleQC moduleQC){
		String sql = querySQL;
		moduleQC.setPrefix("m");
		String whereSQL = moduleQC.getWhereSQL();
		if(whereSQL!=null&&whereSQL.length()>0){
			sql = sql + " WHERE " +whereSQL;
		}
		return this.find(sql);
	}
	
	public void deleteModuleByCodes(List<ModuleModel> moduleModels){
		if(moduleModels==null||moduleModels.isEmpty()){
			return;
		}
		String[] codes = new String[moduleModels.size()];
		for(int i=0;i<moduleModels.size();i++){
			codes[i] = moduleModels.get(i).getCode();
		}
		this.deleteAllBySingleField(ModuleEntity.class, "code", codes);
	}

	public void deleteModuleByIds(Long[] ids){
		this.deleteAllBySingleField(ModuleEntity.class, "id", ids);
	}
	
	public void deleteModuleChildren(Long[] parentIds){
		if(parentIds==null||parentIds.length==0){
			return;
		}
		this.deleteAllBySingleField(ModuleEntity.class, "parentId", parentIds);
	}
	
	public ModuleEntity getModuleEntityByCode(String code){
		String whSql = " WHERE m.code=?";
		String sql = querySQL + whSql;
		List<ModuleEntity> entities = this.find(sql, code);
		return (entities == null||entities.size()==0)?null:entities.get(0);
	}
	
	public long childrenCount(long pId){
		String sql = "SELECT count(m) FROM ModuleEntity m WHERE m.parentId=?";
		return this.counter(sql,pId);
	}
	
	public List<ModuleEntity> getModulesByUserId(long userId){
		return this.find(querySQL);
	}
	
	@SuppressWarnings("rawtypes")
	public List<Long> getChildrenIds(Long[] parentIds){
		if(parentIds==null||parentIds.length==0){
			return null;
		}
		String querySQL = "SELECT m.id FROM t_module m WHERE ";
		StringBuffer where = new StringBuffer();
		int length = parentIds.length;
		for (int i=0;i<length;i++) {
			long id = parentIds[i];
			if(i==length-1){
				where.append("m.parent_id=" + id);
			}
			else{
				where.append("m.parent_id=" + id+" OR ");
			}
		}
		
		String sql = querySQL + where.toString();
		List result = this.executeNativeQuery(sql ,null);
		if(result!=null){
			List<Long> ids = new ArrayList<Long>();
			for (Object object : result) {
				BigInteger bi = (BigInteger)object;
				ids.add(bi.longValue());
			}
			return ids;
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<ModuleModel> getModuleChildren(long parentId){
		String querySQL = "SELECT m.id,m.code,m.name,m.path,m.auth_code,m.status,m.parent_id,(select count(n.id) from t_module n where n.parent_id=m.id) as childrenSize FROM t_module m WHERE m.parent_id=?";
		List result = this.executeNativeQuery(querySQL,new Object[]{parentId});
		return resultToModule(result);
	}
	
	private List<ModuleModel> resultToModule(List result){
		if(result!=null){
			List<ModuleModel> modules = new ArrayList<ModuleModel>(result.size());
			for (Object object : result) {
				Object[] values = (Object[])object;
				ModuleModel model = new ModuleModel();
				model.setId(((BigInteger)values[0]).longValue());
				model.setCode((String)values[1]);
				model.setName((String)values[2]);
				model.setPath((String)values[3]);
				model.setStatus(((Short)values[5]).intValue());
				model.setParentId(((BigInteger)values[6]).longValue());
				model.setHasChildren(((BigInteger)values[7]).intValue()>0);
				modules.add(model);
			}
			return modules;
		}
		return null;
	}
	
}
